package com.kindness.frate.widget;

import android.content.Context;
import android.support.design.widget.CoordinatorLayout;
import android.support.v4.view.ViewCompat;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import java.lang.ref.WeakReference;

/**
 * Created by Administrator on 2017/7/21 0021.
 */

public class ChangeBackBehavior extends CoordinatorLayout.Behavior {

    private WeakReference<View> dependendView;
    private WeakReference<View> childView;
    private int heardSize = -1;

    public ChangeBackBehavior(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean layoutDependsOn(CoordinatorLayout parent, View child, View dependency) {
        if (child != null) {
            childView = new WeakReference<View>(child);
            if (childView.get() instanceof RecyclerView) {
                RecyclerView recyclerView = (RecyclerView) childView.get();
                recyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {
                    @Override
                    public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                        super.onScrollStateChanged(recyclerView, newState);
                    }

                    @Override
                    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                        super.onScrolled(recyclerView, dx, dy);
                        View view = dependendView.get();
                        ViewGroup.LayoutParams params = view.getLayoutParams();
                        int height = (int) child.getTranslationY();
                        int h = height - dy;
                        if (dy > 0) {
                            if (h > 0 && h <= heardSize) {
                                params.height = h;
                                view.setLayoutParams(params);
                                child.setTranslationY(h);
                            }
                        }
                    }
                });
            }
        }
        if (dependency != null && dependency instanceof RelativeLayout) {
            dependendView = new WeakReference<>(dependency);
        }
        return super.layoutDependsOn(parent, child, dependency);
    }

    @Override
    public boolean onLayoutChild(CoordinatorLayout parent, View child, int layoutDirection) {
        child.layout(0, 0, parent.getWidth(), parent.getHeight() - dependendView.get().getHeight());
        if (heardSize <= 0) {
            heardSize = dependendView.get().getHeight();
            child.setTranslationY(heardSize);
        }
        return true;
    }

    @Override
    public boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout, View child, View directTargetChild, View target, int nestedScrollAxes) {
        return nestedScrollAxes == ViewCompat.SCROLL_AXIS_VERTICAL;
    }

    @Override
    public void onNestedPreScroll(CoordinatorLayout coordinatorLayout, View child, View target, int dx, int dy, int[] consumed) {
        View view = dependendView.get();
        ViewGroup.LayoutParams params = view.getLayoutParams();
        int height = (int) child.getTranslationY();
        int h = height - dy;
        if (dy > 0) {
            if (h > 0 && h <= heardSize) {
                params.height = h;
                view.setLayoutParams(params);
                child.setTranslationY(h);
                consumed[1] = dy;
            }
        }
    }

    @Override
    public void onNestedScroll(CoordinatorLayout coordinatorLayout, View child, View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed) {
        if (dyUnconsumed > 0) {
            return;
        }
        View view = dependendView.get();
        ViewGroup.LayoutParams params = view.getLayoutParams();
        int height = (int) child.getTranslationY();
        int h = height - dyUnconsumed;
        if (h >= 0 && h <= heardSize) {
            params.height = h;
            view.setLayoutParams(params);
            child.setTranslationY(h);
        }
    }

    @Override
    public void onStopNestedScroll(CoordinatorLayout coordinatorLayout, View child, View target) {
        childView.get().setScrollY(0);
    }

}
